package com.telecom.cms.dao;
// default package

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.telecom.cms.dto.*;
import com.telecom.cms.util.Constant;

/**
 * Data access object (DAO) for domain model class SysRole.
 * 
 * @see .SysRole
 * @author MyEclipse Persistence Tools
 */

public class SysRoleDAO {
	private static List instlist=null;
	Dao dao = new DaoImpStd();

	String db = (new Constant()).getPara("DATABASE");

	public int insert(SysRole transientInstance) {
		int rs = 0;
		String sql = "select * from  " + db + ".Sys_Role where Role_Id="
				+ transientInstance.getRoleId();
		try {
			ResultSet rs2 = dao.select(sql);
			if (rs2.next())
				sql = "update " + db + ".Sys_Role set role_name='"
						+ transientInstance.getRoleName() + "',role_desc='"
						+ transientInstance.getRoleDesc()+ "' where Role_Id="
						+ transientInstance.getRoleId();
			else
				sql = "insert into " + db + ".Sys_Role values(, '"
						+ transientInstance.getRoleName() + "' , '"
						+ transientInstance.getRoleDesc() + "')";
			rs = dao.insert(sql);
			
			
			if (rs2 != null) {

				rs2.close();
			}

		} catch (Exception re) {
			System.out.println("save failed:" + re);
		}
		finally
		{
			dao.closeconn();
		}
		findAllForce();
		return rs;
	}
	
	public void delete(SysRole persistentInstance) {

		String sql = "delete from " + db + ".Sys_Role where Role_Id = "
				+ persistentInstance.getRoleId();

		try {
			dao.delete(sql);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		findAllForce();
	}
	
	public List findAllForce() {
		String sql = "select * from " + db + ".Sys_Role ";
		List list = new ArrayList();
		try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				SysRole sd = new SysRole();
				sd.setRoleId(rs.getInt(1));
				sd.setRoleName(rs.getString(2));
				sd.setRoleDesc(rs.getString(3));
				list.add(sd);
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}
		instlist=list;
		return list;
	}
	
	public List findAll() {
		if(instlist==null)
			findAllForce();
		return instlist;
	}
	
	public SysRole findById(int roleid) {
		//String sql = "select * from " + db + ".Sys_Role where role_id="+roleid;
		
		SysRole sd = null;
		if(instlist==null)
			findAllForce();
		for(int i=0;i<instlist.size();i++)
		{
			sd = (SysRole)instlist.get(i);
			//System.out.println(sd.getMenuGroupId()+"=?="+menugroupid);
			if(sd.getRoleId()==roleid)
				return sd;
		}
		/*try {
			ResultSet rs = dao.select(sql);
			while (rs.next()) {
				sd = new SysRole();
				sd.setRoleId(rs.getInt(1));
				sd.setRoleName(rs.getString(2));
				sd.setRoleDesc(rs.getString(3));
			}
			if (rs != null) {

				rs.close();
			}

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally
		{
			dao.closeconn();
		}*/
		return sd;
	}
}